exports.install = function (Vue, options) {
  /**
   * 验证手机号码
   * @param phone:手机号
   * @returns {boolean}
   */
  Vue.prototype.is_mobile = function (phone) {
    var reg = /^((\(\d{2,3}\))|(\d{3}\-))?(13|15|18|14|17|19)\d{9}$/;//手机
    return reg.test(phone);
  },
    /**[A-Za-z0-9_\-\u4e00-\u9fa5]+
     * 身份证格式验证
     * card: 身份证号
     */
    Vue.prototype.is_card = function (card) {
      var reg = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
      var card = card.match(reg);
      if (card != null) {
        return true;  //正确
      } else {
        return false; //错误
      }
    },
    /**
     * 时间戳转换日期
     * time：时间戳
     * flag：是否显示时分秒（false：不显示，true：显示）
     * **/
    Vue.prototype.stampToDate = function (time, flag=0) {
        if(time=='' || time==0) return '';
      let date = new Date(time * 1000);
      let Y = date.getFullYear();
      let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
      let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
      let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
      let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
      let s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());

      if (flag==1) {
        return Y+ '-' + M+ '-' + D + h + m + s;
      } else if(flag==2){
        return Y + "-"+M ;
      }else {
            return Y+ '-' + M+ '-' + D;
        }
    },
    /**
     * 去除字符串空格
     * str:字符串
     * is_global:('true'表示去除所有空格，否则只去除首尾空格)
     * */
    Vue.prototype.Trim = function (str, is_global) {
      var result;
      result = str.replace(/(^\s+)|(\s+$)/g, "");
      if (is_global) {
        result = result.replace(/\s/g, "");
      }
      return result;
    },
    /**格式化银行卡4位空一格
     * v : 银行卡号
     * */
    Vue.prototype.formates = function (v) {
      if (/\S{5}/.test(v)) {
        v = v.replace(/\s/g, '').replace(/(\d{4})(?=\d)/g, "$1 ");
      }

      return v;
    },
    /**
     *获取 字符长度
     * str:字符串（中文:2个字符，英文:1个字符）
     * **/
    Vue.prototype.getStrLength = function (str) {
      var realLength = 0, len = str.length, charCode = -1;

      for (var i = 0; i < len; i++) {
        charCode = str.charCodeAt(i);
        if (charCode >= 0 && charCode <= 128) realLength += 1;
        else realLength += 2;
      }
      return realLength;
    },

    /**
     * 保留两位小数
     * @param number 转换数字
     * @param d 保留位数
     * @returns {string}
     */
    Vue.prototype.toFixed = function (number, d = 2) {
      var num = parseFloat(number);
      num=num.toFixed(d);

      if(d==1) num=num+'0';

      return num;
    },

    /**
     * 手机号影藏中间几位楼
     * @param mobile 手机号
     * @returns {string}
     */
    Vue.prototype.subMobie = function (mobile) {
      var c=mobile.toString();


      return c.substr(0,3)+"****"+c.substr(7,4);
    },

    /**
     * 拼接图片地址
     */
    Vue.prototype.img = function (url,other='') {

        var img=process.env.IMG_URL;
        var src=img+url;

        if(other!=null && other!=''){
            src=img+other;
        }

        return src;
    },

      Vue.prototype.getCity=function (v) {
          var arr={
              '京':'北京市',
              '津':'天津市',
              '沪':'上海市',
              '渝':'重庆市',
              '冀':'河北省',
              '晋':'山西省',
              '辽':'辽宁省',
              '吉':'吉林省',
              '黑':'黑龙江省',
              '苏':'江苏省',
              '浙':'浙江省',
              '皖':'安徽省',
              '闽':'福建省',
              '赣':'江西省',
              '鲁':'山东省',
              '豫':'河南省',
              '鄂':'湖北省',
              '湘':'湖南省',
              '粤':'广东省',
              '琼':'海南省',
              '川':'四川省',
              '贵':'贵州省',
              '云':'云南省',
              '陕':'陕西省',
              '甘':'甘肃省',
              '青':'青海省',
              '藏':'西藏',
              '桂':'广西',
              '蒙':'内蒙',
              '宁':'宁夏',
              '新':'新疆',
              '澳':'澳门',
              '台':'台湾省',
          }

          return arr[`${v}`];
      },


      /**
       * 图片转Base64
       */
      Vue.prototype.getBase64Image = function(img,sq=200){
          var canvas = document.createElement("canvas");
          var square = sq;
          if (img.width > img.height) {
              canvas.width = Math.round(square * img.width / img.height);
              canvas.height = square;
          } else {
              canvas.width = square;
              canvas.height = Math.round(square * img.height / img.width);
          }

          var context = canvas.getContext("2d");
          context.clearRect(0, 0, canvas.width, canvas.height);
          var imageWidth;
          var imageHeight;
          if (img.width > img.height) {
              imageWidth = Math.round(square * img.width / img.height);
              imageHeight = square;
          } else {
              imageWidth = square;
              imageHeight = Math.round(square * img.height / img.width);
          }

          context.drawImage(img, 0, 0, imageWidth, imageHeight);
          var ext = img.src.substring(img.src.lastIndexOf(".") + 1).toLowerCase();
          var dataURL = canvas.toDataURL("image/" + ext);
          return dataURL;
      }

    /**
     * 计算时间
     */
    Vue.prototype.getCarRefrsh = function(time){
        var result="";
        dateTimeStamp=time*1000;
        if(dateTimeStamp>0){
            var minute = 1000 * 60;      //把分，时，天，周，半个月，一个月用毫秒表示
            var hour = minute * 60;
            var day = hour * 24;
            var week = day * 7;
            var halfamonth = day * 15;
            var month = day * 30;
            var now = new Date().getTime();   //获取当前时间毫秒
            var diffValue = now - dateTimeStamp;//时间差

            if(diffValue < 0){
                return;
            }
            var minC = diffValue/minute;  //计算时间差的分，时，天，周，月
            var hourC = diffValue/hour;
            var dayC = diffValue/day;
            var weekC = diffValue/week;
            var monthC = diffValue/month;
            if(monthC >= 1 && monthC <= 3){
                result = " " + parseInt(monthC) + "月前"
            }else if(weekC >= 1 && weekC <= 3){
                result = " " + parseInt(weekC) + "周前"
            }else if(dayC >= 1 && dayC <= 6){
                result = " " + parseInt(dayC) + "天前"
            }else if(hourC >= 1 && hourC <= 23){
                result = " " + parseInt(hourC) + "小时前"
            }else if(minC >= 1 && minC <= 59){
                result =" " + parseInt(minC) + "分钟前"
            }else if(diffValue >= 0 && diffValue <= minute){
                result = "刚刚"
            }else {
                var datetime = new Date();
                datetime.setTime(dateTimeStamp);
                var Nyear = datetime.getFullYear();
                var Nmonth = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
                var Ndate = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
                var Nhour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
                var Nminute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
                var Nsecond = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
                result = Nyear + "-" + Nmonth + "-" + Ndate
            }
        }

        return result;
    }
};
